import {
	API_BASE_URL,
	API_TIMEOUT_MILLI
} from '@/config.js'
import store from '../store'

export const request = (options) => {
	return new Promise((resolve, reject) => {
		const token = store.state.user.token
		const header = {
			'Content-Type': 'application/json',
			'Authorization': `Bearer ${token}`,
		}
		uni.request({
			timeout: options.timeout ? options.timeout : API_TIMEOUT_MILLI,
			url: `${API_BASE_URL}${options.url}`,
			method: options.method ? options.method.toUpperCase() : 'GET',
			data: options.data || {},
			header: header,
			success(res) {
				resolve(res.data)
			},
			fail: (err) => {
				resolve(err)
				console.log(err)
			}
		})
	})
}